home *** CD-ROM | disk | FTP | other *** search
- Path: wkaufman.us.oracle.com!wkaufman
- From: wkaufman@wkaufman.us.oracle.com (William Kaufman)
- Newsgroups: comp.lang.c
- Subject: Re: [Q] functions returning structures
- Date: 3 Mar 1996 18:10:05 GMT
- Organization: Oracle Corporation, Redwood Shores CA
- Message-ID: <4hcn9u$9fo@inet-nntp-gw-1.us.oracle.com>
- References: <4hasjj$opf@decaxp.harvard.edu>
- NNTP-Posting-Host: wkaufman.us.oracle.com
-
- In article <4hasjj$opf@decaxp.harvard.edu> martino@course2.harvard.edu (Carlo Martino) writes:
- ] Is it bad for a function to return a structure?
-
- Not "bad"--it's part of the ANSI Standard. Just make sure the
- structure is defined before the function is defined and declared, and
- that the function is properly declared before it's used.
-
- The only problem with structure passing that I know of is that a lot
- of pre-ANSI compilers don't accept structure passing (either into or out
- of a function). The standard work-around for that is to pass in the
- address of the caller's structure, like,
-
- RGB rgb;
- void foo(/* RGB * */);
-
- foo(&rgb);
-
- and have the callee assign through the pointer, like,
-
- void foo(rgbP)
- RGB *rgbP;
- {
- rgbP->r = rgbP->b = 127;
- rgbP->g = 0;
- }
-
- ] I have been programming in
- ] C for upwards of 6 years, and only recently did I hear something to the effect
- ] that structures returned by functions have a tendency to be corrupted.
-
- I've never seen that happen on a compiler that supported structure
- passing. Maybe the structure wasn't defined or function declared
- properly: try turning your compiler's warnings all the way up--maybe
- there wasn't a structure definition and the compiler pretended it was an
- int,...
-
- -- Bill K.
-
- Bill Kaufman, | "Patience is a virtue. Seersucker is a fabric."
- wkaufman@us.oracle.com | -- Bazooka Joe
-